/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.merge;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import sync.exceptions.MergeException;
import sync.exceptions.MergeRuntimeException;
import utilities.IO;


/**
 *
 * @author zalem1
 */
public class WatchEntry {
    private String _localPath;
    private IMergeProvider _localContent;
    private IMergeProvider _onlineContent;

    public WatchEntry(String localPath, IMergeProvider localMedia, IMergeProvider onlineMedia) {
       if (_localContent == null) throw new MergeRuntimeException("LocalContent cannot be null.");
       if (_onlineContent == null) throw new MergeRuntimeException("OnlineContent cannot be null.");
        
       _localContent = localMedia;
       _onlineContent = onlineMedia;

       _localPath = localPath;
    }

    /**
     * @return the _localPath
     */
    public String getLocalPath() {
        return _localPath;
    }
    
    public boolean existsLocally() throws MergeException {
        return _localContent.exists(_localPath);
    }

    public boolean existsOnline() throws MergeException {
        return _onlineContent.exists(_localPath);
    }
    
    public boolean changedLocally() throws MergeException {
        return _localContent.isChanged(_localPath);
    }

    public boolean changedOnline() throws MergeException {
        return _onlineContent.isChanged(_localPath);
    }
    
    public long getOnlineSize() {
        return _onlineContent.getSize(_localPath);
    }   
    
    public long getLocalSize() {
        return _localContent.getSize(_localPath);
    }
    
    public String getFilename() {
        String filename = _localContent.getFilename(_localPath);
        
        if (filename == null || filename.isEmpty())
            filename = _onlineContent.getFilename(_localPath);
        
        return filename;
    }
    
    public boolean isNew() {
        boolean isNewOnline = _onlineContent.isNew(_localPath);
        boolean isNewLocally = _localContent.isNew(_localPath);
                
        return isNewOnline && isNewLocally;
    }
    
    public boolean compareLocalToOnline() throws MergeException {
        long localFileSize = _localContent.getSize(_localPath);
        long onlineFileSize = _onlineContent.getSize(_localPath);
        
        boolean match = (localFileSize == onlineFileSize);
        if (!match) return false;
        
        String localHash = _localContent.getMediaHash(_localPath);
        String onlineHash = _onlineContent.getMediaHash(_localPath);

        match = (onlineHash.equals(localHash));
        return match;
    }

    public void copyOnlineToLocal(boolean overwrite) throws MergeException {
        InputStream in = _onlineContent.getInputStream(_localPath, 0);
        OutputStream out = _localContent.getOutputStream(_localPath);

        try {
            IO.copy(in, out);
        } catch (IOException ex) {
            throw new MergeException("Cannot copy remote resource to the local location.", ex);
        }
    }

    void copyLocalToOnline(boolean overwrite) throws MergeException {
        InputStream in = _localContent.getInputStream(_localPath, 0);
        OutputStream out = _onlineContent.getOutputStream(_localPath);

        try {
            IO.copy(in, out);
        } catch (IOException ex) {
            throw new MergeException("Cannot copy local resource to the remote location.", ex);
        }
    }
    
    public void updateMetadata() throws MergeException {
        _localContent.updateMetadata(_localPath);
        _onlineContent.updateMetadata(_localPath);
    }

    void deleteMedia() throws MergeException {
        if (_localContent.exists(_localPath))
            _localContent.delete();

        if (_onlineContent.exists(_localPath))
            _onlineContent.delete();
    }
}
